package com.wn.controller;

import com.wn.bean.Constants;
import com.wn.bean.DataRoleBean;
import com.wn.bean.R;
import com.wn.bean.Role;
import com.wn.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellow Docter
 * @since 2022-05-16
 */
@RestController
@RequestMapping("/roles")
@Slf4j
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @GetMapping
    public R queryAll(){

        List<Role> roles = roleService.list();

         R r = new R();

        if (!CollectionUtils.isEmpty(roles)){
            r.setData(roles);
            r.setMsg("操作成功");
            r.setCode(Constants.SUCCESS_CODE);
            return r;
        }
        return R.ERROR("没有数据");
    }

    @PostMapping
    public R addRole(@RequestBody DataRoleBean dataRoleBean){

        return roleService.addRole(dataRoleBean);
    }
}
